<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList">
        <template slot-scope="scope" slot="menuLeft">
          <el-button type="primary"
                     size="small"
                     @click.stop="handleBatchSubmit()">提 交
          </el-button>
          <approve-button
            :approve-before="approveBefore"
            @approve="handleApprove">
          </approve-button>
          <el-button type="primary"
                     size="small"
                     @click.stop="handleExport()">导 出
          </el-button>
          <el-button type="primary"
                     size="small"
                     :hidden="true"
                     v-if="false"
                     @click.stop="handlePrint()">打 印
          </el-button>
        </template>
        <template slot="menu" slot-scope="scope">
          <el-button
            type="text"
            size="small"
            icon="el-icon-tickets"
            v-if="scope.row.isDecompose=='0'&&(scope.row.checkStatus=='0'||scope.row.checkStatus=='2')"
            @click.stop="handleDecompose(scope.row,scope.index)">分 解
          </el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-edit"
            v-if="scope.row.checkStatus=='0'||scope.row.checkStatus=='3'"
            @click.stop="handleApply(scope.row,scope.index)">申 请
          </el-button>
        </template>
      </avue-crud>
      <open-dialog title="分解" :DialogVisible.sync="dialogVisible">
        <avue-form v-model="form" :option="formOption" v-if="dialogVisible"></avue-form>
        <dynamic-crud :data="decomposeData" :option="decomposeOption" v-if="dialogVisible"></dynamic-crud>
        <template slot="footer">
          <el-button type="primary" size="small" @click="submitDecompose">确 认</el-button>
          <el-button size="small" @click="dialogVisible=false">取 消</el-button>
        </template>
      </open-dialog>
    </basic-container>
  </div>
</template>

<script>
  import baseCrud from '@/mixins/base-crud'
  import { decompose } from '@/api/subway/swstproduceplantempdecompose'
  import { startDate, endDate } from "@/const/crud/subway/swstproduceplantempdecompose";

  export default {
    name: 'StPlanProduceTempDecompose',
    mixins: [baseCrud('subway/swstproduceplantempdecompose')],
    data() {
      return {
        params: {
          //planType: 2 ,// 生产计划
          isSend: '1' // 已下发
        },
        searchForm: {
          planStartTime: startDate,
          planEndTime: endDate
        },
        dialogVisible: false,
        decomposeData: [],
        decomposeOption: {
          column: [{
            label: '起止号',
            prop: 'startEndNo',
            cell: true,
            rules: [{
              required: true,
              message: '请输入起号/止号',
              trigger: 'blur'
            }]
          }, {
            label: '张数',
            prop: 'ticketNumber',
            type: 'number',
            cell: true,
            rules: [{
              required: true,
              message: '请输入车票张数',
              trigger: 'blur'
            }]
          }, {
            label: '车票编码员',
            prop: 'ticketCoder',
            type: 'select',
            cell: true,
            component: 'user-select',
            params: {
              userCodeKey: 'ticketCoderCode'
            },
            rules: [{
              required: true,
              message: '请输入车票编码员',
              trigger: 'blur'
            }]
          }, {
            label: '车票编码员工号',
            prop: 'ticketCoderCode',
            rules: [{
              required: true,
              message: '请输入配送员工号',
              trigger: 'blur'
            }]
          }, {
            label: '计划开始时间',
            prop: 'planStartTime',
            type: 'date',
            cell: true,
            format: 'yyyy-MM-dd',
            valueFormat: 'yyyy-MM-dd HH:mm:ss'
          }, {
            label: '计划结束时间',
            prop: 'planEndTime',
            type: 'date',
            cell: true,
            format: 'yyyy-MM-dd',
            valueFormat: 'yyyy-MM-dd HH:mm:ss'
          }, {
            label: '备注',
            prop: 'remark',
            cell: true
          }]
        }
      }
    },
    computed: {
      permissionList() {
        return {}
      },
      formOption(){
        let formOption = this.deepClone(this.tableOption)
        formOption.boxType = 'edit'
        formOption.menuBtn = false
        if(this.form.ticketNumberNow){
          formOption.group[0].display = true
          formOption.group[0].column.forEach(column => {
            column.disabled = true
          })
          formOption.group[2].display = true
          formOption.group[2].column.forEach(column => {
            column.disabled = true
          })
        }else {
          formOption.group[0].display = false
          formOption.group[2].display = false
        }
        return formOption
      }
    },
    methods: {
      handleDecompose(row, index){
        this.form = row
        this.decomposeData = []
        this.dialogVisible = true
      },
      handleApply(row, index){
        //row.isDecompose = '1';
        this.$refs.crud.rowEdit(row, index)
      },
      submitDecompose(){
        let ticketNumber = this.form.ticketNumber;
        let ticketNumberNow = this.form.ticketNumberNow;
        let arr = this.decomposeData;
        let sumNum = 0;
        Object.keys(arr).forEach((item) => {
          sumNum +=arr[item].ticketNumber;
        })
        //区分原张数和现计划张数
        debugger
        if(ticketNumberNow != "" && ticketNumberNow != null && ticketNumberNow != undefined){
          if(ticketNumberNow != sumNum){
            this.$message.error('现计划张数和要分解的张数不相等！');
            return false;
          }
        }else{
          if(ticketNumber != sumNum){
            this.$message.error('张数和要分解的张数不相等！');
            return false;
          }
        }
        let params = {
          id: this.form.id,
          decomposeList: this.decomposeData
        }
        decompose(params).then(response => {
          this.$message.success('分解成功')
          this.dialogVisible = false
          this.getList(this.page)
        })
      },
      approveBefore(callback){
        var tableSelect = this.$refs.crud.tableSelect
        if(tableSelect.length == 0){
          this.$message.info('请选择要审批的记录')
        }else{
          for (const table of tableSelect) {
            if (table.checkStatus != "1") {
              this.$message.error("请选择已提交的数据");
              return false;
            }
          }
          callback()
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
</style>
